package com.usian.behavior.service.impl;

import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl extends BaseServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Autowired
    private ApBehaviorEntryService entryService;

    @Override
    public ResponseResult saveFollowBehavior(Integer followId, Integer currentUserId, Long articleId) {
        if (followId == null || currentUserId == null || articleId == null) return paramError;
        ApBehaviorEntry entry = entryService.findByUserIdOrEquipmentId(currentUserId, null);
        if (entry == null) {
            entry = new ApBehaviorEntry();
            entry.setCreatedTime(new Date());
            entry.setEntryId(currentUserId);
            entry.setType((short) 1);
            entryService.getBaseMapper().insert(entry);
//            entryService.save(entry);同上
        }
        //保存关注行为
        ApFollowBehavior alb = new ApFollowBehavior();
        alb.setEntryId(entry.getId());
        alb.setCreatedTime(new Date());
        alb.setArticleId(articleId);
        alb.setFollowId(followId);
        save(alb);
        return okResult;
    }
}
